import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.util.ArrayList;

/**
 * @author lys
 * 更新类别
 */

public class UpdateType extends JFrame {
    private static UpdateType instance;

    private JLabel jLabel = new JLabel("更新类别");
    private JLabel jLabel1 = new JLabel(" 类别 :");
    private JLabel jLabel2 = new JLabel("新类别:");

    private JComboBox<String> box = new JComboBox<String>();

    private JTextArea jTextArea1 = new JTextArea();

    private JButton button1 = new JButton("确认更改");
    private JButton button2 = new JButton("返回");

    private Font font1 = new Font("宋体",Font.BOLD,60);
    private Font font2 = new Font("宋体",Font.BOLD,40);
    private Font font3 = new Font("宋体",Font.BOLD,30);

    private JPanel jPanel1 = new JPanel();
    private JPanel jPanel2 = new JPanel();

    private UpdateType() {
        Icon i = new ImageIcon(UpdateType.class.getResource("img/login2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 800, 800);

        jLabel.setFont(font1);
        jLabel.setBounds(280, 50, 300, 80);
        jLabel.setForeground(Color.white);

        //类别
        jLabel1.setFont(font2);
        jLabel1.setBounds(150,270,200,50);
        jLabel1.setForeground(Color.white);
        box.setPreferredSize(new Dimension(350,50));

        ArrayList<String> typeList = AddBook.getTypeList();
        for(String type: typeList){
            box.addItem(type);
        }

        box.setFont(font3);
        //box.setBounds(400,200,200,40);
        box.setOpaque(false);
        jPanel1.setBounds(0,230,800,60);
        jPanel1.add(jLabel1);
        jPanel1.add(box);

        //新类别
        jLabel2.setFont(font2);
        jLabel2.setBounds(150, 360, 200, 50);
        jLabel2.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jTextArea1.setPreferredSize(new Dimension(350,50));
        jTextArea1.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea1.setFont(font2);
        jTextArea1.setForeground(Color.black);
        jPanel2.setBounds(0,360,800,60);
        jPanel2.add(jLabel2);
        jPanel2.add(jTextArea1);

        button1.setBounds(280,550,300,60);
        button1.setFont(font2);
//        button1.setForeground(Color.white);
        button2.setBounds(620,650,100,50);
//        button2.setForeground(Color.white);
        button2.setFont(font3);

        jPanel1.setOpaque(false);
        jPanel2.setOpaque(false);

        jTextArea1.setOpaque(false);

        add(jLabel);
        add(jPanel1);
        add(jPanel2);
        add(button1);
        add(button2);
        add(Label);

        setSize(800,800);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String oldType = (String)box.getSelectedItem();
                String newType = jTextArea1.getText();
                updateType(newType, oldType);
                ManageBook.getInstance().showBooks();
                removeInstance();
                AddBook.removeInstance();
                DeleteBook.removeInstance();
                UpdateBook.removeInstance();
                DeleteType.removeInstance();
                AddType.removeInstance();
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        button1.setFocusPainted(false);
        button2.setFocusPainted(false);
    }
    public static UpdateType getInstance() {
        if(instance==null) instance=new UpdateType();
        return instance;
    }

//    public static void main(String[] args) {
//        UpdateType.getInstance();
//    }
    /**
     * @author zj
     * 在数据库中更新类别
     */
    public void updateType(String newType, String oldType){
        if(newType.length() == 0){
            JOptionPane.showMessageDialog(null, "输入信息不能为空！");
            return;
        }
        String sql = "update typelist set type = ? where type = ?";
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1, newType);
            preSql.setString(2, oldType);
            preSql.executeUpdate();
            sql = "update book set type = ? where type = ?";
            preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1, newType);
            preSql.setString(2, oldType);
            preSql.executeUpdate();
            JOptionPane.showMessageDialog(null, "修改类别成功");
        }catch (Exception e){
            System.out.println(e);
        }
        setVisible(false);
        AdminInterface.getInstance().admin.setVisible(true);
    }
    public static void removeInstance(){
        instance = null;
    }
}
